package com.tms.app.tms.ui;

import android.content.Intent;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tms.app.tms.R;
import com.tms.app.tms.adapter.FeeInfoAdapter;
import com.tms.app.tms.adapter.GoodsInfoAdapter;
import com.tms.app.tms.app.Constant;
import com.tms.app.tms.bean.Fee;
import com.tms.app.tms.bean.GoodsInfo;
import com.tms.app.tms.bean.response.BaseResponse;
import com.tms.app.tms.bean.response.FeeInfoRes;
import com.tms.app.tms.bean.response.GoodsRes;
import com.tms.app.tms.ui.base.presenter.AbsServicexUtils;
import com.tms.app.tms.ui.base.presenter.BaseListPresenter;

import org.xutils.http.RequestParams;

/**
 * Created by Administrator on 2017/12/19 0019.
 */

public class GoodsPresenter extends BaseListPresenter {
    private int dispatchId;

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        BaseResponse baseResponse=gson.fromJson(result,BaseResponse.class);
        if (requestType==1){
            GoodsRes goodsRes=gson.fromJson(result,GoodsRes.class);
            sendMsgToView(goodsRes);
          //  bindDataToView(goodsRes);
        }
        if (requestType==2){
            baseResponse.setWorkType(2);
            sendMsgToView(baseResponse);
        }
        if (requestType==3){
            baseResponse.setWorkType(3);
            sendMsgToView(baseResponse);
        }


    }

    private void bindDataToView(GoodsRes goodsRes) {
        if (goodsRes.getCode()==200&&goodsRes.getData()!=null&&goodsRes.getData().size()>0){
            GoodsInfoAdapter adapter=iView.getAdapter(0);
            adapter.getData().clear();
            adapter.getData().addAll(goodsRes.getData());
            adapter.notifyDataSetChanged();
            iView.refreshComplite();
            return;
        }
        iView.refreshNoData();
        if (goodsRes.getCode()!=200)
            iView.showMsg(goodsRes.getError());
    }


    public void getGoodsList(String token,int dispatchId){
        this.token=token;
        this.dispatchId=dispatchId;
        requestType=1;
        RequestParams params=new RequestParams(Constant.GoodsList);
        params.addBodyParameter("token",token);
        params.addBodyParameter("dispatchId",dispatchId+"");
       // params.addBodyParameter("showLoading", AbsServicexUtils.showAndHideLoading);
        getObjectFromServer(params,1);

    }

    public void updateGoodsList(GoodsRes goodsRes){
        requestType=2;
        RequestParams params=new RequestParams(Constant.GoodsEdit);
        params.setAsJsonContent(true);
        params.setBodyContent(gson.toJson(goodsRes));
        getObjectFromServer(params,1);

    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        getGoodsList(token,dispatchId);
    }

    /******
     * 删除货物
     * @param token
     * @param goodId
     */
    public void delGoods(String token,int goodId){
        requestType=3;
        this.token=token;
        RequestParams params=new RequestParams(Constant.GoodsDelte);
        params.addBodyParameter("token",token);
        params.addBodyParameter("goodId",goodId+"");
        getObjectFromServer(params,1);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        super.onItemChildClick(adapter, view, position);
        GoodsInfo goodsInfo= (GoodsInfo) adapter.getData().get(position);
        switch (view.getId()){
            case R.id.text_del:
                delGoods(token,goodsInfo.getGoodId());
                break;
            case R.id.text_edit:
                Intent intent=new Intent(view.getContext(), AddGoodsActivity.class);
                intent.putExtra("goodInfo",gson.toJson(goodsInfo));
                intent.putExtra("dispatchId",dispatchId);
                iView.startActivityForResult(intent,1);
                break;
        }
    }
}
